package org.hgvc.webapp.ets.jsf.beans.admin;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.AgreementControl;
import org.hgvc.webapp.ets.spring.service.AgentService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class AdminAgreementControlBean extends AbstractBean {

	public static final String BEAN_NAME="adminAgreementControlBean";
	public static final String NAV_LIST="adminAgentAgreementMgmt";
	public static final String NAV_CREATE="adminCreateAgentAgreement";
	public static final String NAV_EDIT="adminEditAgentAgreement";
	
	
	private AgentService service;
	private Agent agent;
	private AgreementControl control;
	private List<AgreementControl> controlList;
	
	public Agent getAgent() {
		return agent;
	}
	public void setAgent(Agent agent) {
		this.agent = agent;
	}
	public void setService(AgentService service) {
		this.service = service;
	}
	
	public void setControlList(List<AgreementControl> controlList) {
		this.controlList = controlList;
	}
	public AgreementControl getControl() {
		return control;
	}
	public void setControl(AgreementControl control) {
		this.control = control;
	}
	
	/*-----------------------------------------------------------*/
	
	
	public String navListAction(Agent agent){
		this.agent = agent;
		return NAV_LIST.concat("_"+getRoleName());
	}
	
	public String backToList(){
		return NAV_LIST.concat("_"+getRoleName());
	}
	
	public List<AgreementControl> getControlList() {
		try{
			controlList = service.getAgreementControlList(agent);
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return controlList;
	}
	
	public String navCreateAction(){
		control = new AgreementControl();
		control.setAgent(agent);
		return NAV_CREATE.concat("_"+getRoleName());
	}
	
	public String createAction(){
		try{
			service.createAgreementControl(control, getThisUser());
			return NAV_LIST.concat("_"+getRoleName());
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_CREATE.concat("_"+getRoleName());
	}
	
	public void editActionListener(ActionEvent actionEvent){
		UIParameter parameter = (UIParameter)actionEvent.getComponent().findComponent("agreementId");
		String id = (String)parameter.getValue();
		for(AgreementControl c:controlList){
			if(c.getAgreementControlId().equalsIgnoreCase(id)){
				control = c;
				break;
			}
		}
	}
	
	public String navEditAction(){
		return NAV_EDIT.concat("_"+getRoleName());
	}
	
	public String editAction(){
			try{
				service.editAgreementControl(control, getThisUser());
				return NAV_LIST.concat("_"+getRoleName());
			}catch(BaseException be){
				be.printStackTrace();
				initErrorBean(be);
			}
			return NAV_EDIT.concat("_"+getRoleName());
	}
	
	public String deleteAction(){
		try{
			List<AgreementControl> delControlList = new ArrayList<AgreementControl>();
			for (AgreementControl ac : controlList){
				if (ac.isMarkAsAction())
					delControlList.add(ac);
			}
			service.deleteAgreementControl(delControlList, getThisUser());
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_LIST.concat("_"+getRoleName());
	}
}
